package com.demo.app.module.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
	
	public static Map<String,RoleInfo> map = new HashMap<String,RoleInfo>();
	
	@RequestMapping("/userlist")
	public String userlist(Model model) {
		
		List<RoleInfo> list = new ArrayList<RoleInfo>();
		
		for(Map.Entry<String, RoleInfo> entry : map.entrySet()) {
			list.add(entry.getValue());
		}
		
		model.addAttribute("roles", list);
		model.addAttribute("size", list.size());
		
		return "/user/admin-role";
	}
	
	
	@RequestMapping("/save")
	@ResponseBody
	public ResultVo saveRole(RoleInfo role) {
		
		if(null != role) {
			if(null == role.getId() || "".equals(role.getId())) {
				String key = String.valueOf(System.currentTimeMillis());
				role.setId(key);
				map.put(key, role);
			}else {
				map.put(role.getId(), role);
			}
			return ResultVo.success();
		}else {
			return ResultVo.msg("-1", "添加失败");
		}
	}
	
	
	@RequestMapping("/adduser")
	public String adduser(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		if("".equals(id) || null == id) {
			return "/user/admin-role-add";
		}else {
			
			RoleInfo role = map.get(id);
			model.addAttribute("role", role);
			return "/user/admin-role-add";
		}
	}
	
	
	@RequestMapping("/delRole")
	@ResponseBody
	public ResultVo delRole(HttpServletRequest request) {
		String id = request.getParameter("id");
		if(null != id && !"".equals(id)) {
			String [] ids = id.split(",");
			for(String key : ids) {
				map.remove(key);
			}
			return ResultVo.success();
		}else {
			return ResultVo.msg("-1", "删除失败");
					
		}
	}
	
	@RequestMapping("/userrole")
	public String userrole(Model model) {
		List<RoleInfo> list = new ArrayList<RoleInfo>();
		
		RoleInfo role = new RoleInfo();
		role.setId("1");
		role.setName("系统管理员");
		role.setDesc("拥有至高无上的权利");
		
		RoleInfo role1 = new RoleInfo();
		role1.setId("2");
		role1.setName("文档管理员");
		role1.setDesc("具有添加、审核、发布、删除内容的权限");
		
		RoleInfo role2 = new RoleInfo();
		role2.setId("3");
		role2.setName("用户管理员");
		role2.setDesc("对系统用户进行管理");
		
		RoleInfo role3 = new RoleInfo();
		role3.setId("4");
		role3.setName("程序管理员");
		role3.setDesc("对系统的程序进行修改维护");
		
		
		list.add(role);
		list.add(role1);
		list.add(role2);
		list.add(role3);
		model.addAttribute("roles", list);
		
		return "user/admin-role";
	}
	
	
	

}
